package webapp.mvc.controllers.forms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class SimpleResponse {
    public static final int OK = 0;
    public static final int COMMON_FAILURE = 100;
    private int code;
    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public SimpleResponse() {
        this.code = OK;
    }
    
    public static SimpleResponse ok(Object data) {
        return new SimpleResponse(SimpleResponse.OK, data);
    }
    
    public static SimpleResponse fail(Object data) {
        return new SimpleResponse(SimpleResponse.COMMON_FAILURE, data);
    }

    public static SimpleResponse error(Exception e) {
        return fail(e.getMessage());
    }
    
    public SimpleResponse(int code, Object data) {
        this.code = code;
        this.data = data;
    }
    public boolean isSuccess() {
        return OK == this.code;
    }
    public String toJSON() {
        return ((JSONObject) JSON.toJSON(this)).toJSONString();
    }
}
